package study.dq.unit.test.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import study.dq.unit.test.model.User;
import study.dq.unit.test.until.DruidConnection;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class UserDto implements IUserDao {

    @Override
    public User add(User user) {
        QueryRunner runner = new QueryRunner(DruidConnection.getInstance().getDatasource());
        ResultSetHandler<Object[]> h = new ResultSetHandler<Object[]>() {
            public Object[] handle(ResultSet rs) throws SQLException {
                if (!rs.next()) {
                    return null;
                }

                ResultSetMetaData meta = rs.getMetaData();
                int cols = meta.getColumnCount();
                Object[] result = new Object[cols];

                for (int i = 0; i < cols; i++) {
                    result[i] = rs.getObject(i + 1);
                }

                return result;
            }
        };
        try {
            Object[] result = runner.insert("insert into t_user(username,password,nickname) values (?,?,?)",h,user.getUsername(),user.getPassword(),user.getNickname());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return load(user.getUsername());
    }

    @Override
    public void delete(String username) {
        QueryRunner runner = new QueryRunner(DruidConnection.getInstance().getDatasource());
        try {
            int rows = runner.update("delete from t_user where username = ?",username);
            System.out.println(rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public User load(String username) {
        ResultSetHandler<User> handler = new BeanHandler<>(User.class);
        QueryRunner runner = new QueryRunner(DruidConnection.getInstance().getDatasource());
        try {
            return runner.query("select * from t_user where username = ?",handler,username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User update(String username, String password, String nickname) {
        QueryRunner runner = new QueryRunner(DruidConnection.getInstance().getDatasource());
        ResultSetHandler<Object[]> h = new ResultSetHandler<Object[]>() {
            public Object[] handle(ResultSet rs) throws SQLException {
                if (!rs.next()) {
                    return null;
                }

                ResultSetMetaData meta = rs.getMetaData();
                int cols = meta.getColumnCount();
                Object[] result = new Object[cols];

                for (int i = 0; i < cols; i++) {
                    result[i] = rs.getObject(i + 1);
                }

                return result;
            }
        };
        try {
            Object[] result = runner.insert("update t_user set password=?,nickname=? where username = ?",h,password,nickname,username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return load(username);
    }

    public static void main(String[] args){
        User user = User.builder().username("sss").password("123456").nickname("big S").build();
        UserDto userDto = new UserDto();
        User user1 = userDto.add(user);
        System.out.println(user1.toString());
    }
}
